var getDateStr = function (today, addDayCount) {
  var date;
  if (today) {
    date = new Date(today);
  } else {
    date = new Date();
  }
  date.setDate(date.getDate() + addDayCount); //获取AddDayCount天后的日期 
  var y = date.getFullYear();
  var m = date.getMonth() + 1; //获取当前月份的日期 
  var d = date.getDate();

  var up_m = date.getMonth(); //获取上月
  var up_d = 1; //获取上月1号
  var up_br = date.getDate() + 1; //上月本日
  if (m < up_m) {
    y = y - 1
  }
  if (m < 10) {
    m = '0' + m;
  };
  if (up_m < 10) {
    up_m = '0' + up_m;
  };
  if (d < 10) {
    d = '0' + d;
  };
  if (up_d < 10) {
    up_d = '0' + up_d;
  };
  if (up_br < 10) {
    up_br = '0' + up_br;
  };
  //console.log(y + "-" + m + "-" + d)
  return y + "-" + m + "-" + d;
}


const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatData = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatData: formatData,
  formatTime: formatTime,
  getDateStr: getDateStr
}